package org.jarraiders.musiclibrary.server;

import java.util.ArrayList;

import org.jarraiders.musiclibrary.client.ArtistsService;
import org.jarraiders.musiclibrary.domain.Artist;
import org.jarraiders.musiclibrary.server.service.ArtistServiceDAO;
import org.jarraiders.musiclibrary.server.service.ConverterFabric;
import org.jarraiders.musiclibrary.shared.domain.ArtistDTO;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class ArtistsServiceImpl extends RemoteServiceServlet implements
		ArtistsService {
	private static ArtistServiceDAO service;
	static {
		service = new ArtistServiceDAO();
	}

	@Override
	public ArtistDTO addArtist(ArtistDTO artist) {
		
		Artist artistP = ConverterFabric.getArtistDAO(artist);
		service.addNewEntity(artistP);
		return null;
	}

	@Override
	public ArrayList<ArtistDTO> deleteArtist(String id) {
		
		service.deleteEntity(service.getEntityById(id));
		return getArtistDetails();
	}

	@Override
	public ArtistDTO getArtist(String id) {
		
		return ConverterFabric.getArtistDTO(service.getEntityById(id));
	}

	@Override
	public ArrayList<ArtistDTO> getArtistDetails() {
		
		ArrayList<Artist> list = (ArrayList<Artist>) service.getAllEntities();
		ArrayList<ArtistDTO> listDTO = new ArrayList<ArtistDTO>();
		for (Artist artist : list) {
			listDTO.add(ConverterFabric.getArtistDTO(artist));
		}
		return listDTO;
	}

	@Override
	public ArtistDTO updateArtist(ArtistDTO artist) {
		
		Artist artistP = ConverterFabric.getArtistDAO(artist);
		service.updateEntity(artistP);
		return ConverterFabric.getArtistDTO(service.getEntityById(artistP.getArtistId()));
	}

	@Override
	public ArrayList<ArtistDTO> searchArtist(String searchText) {
		ArrayList <Artist> list = (ArrayList<Artist>) service.searchEntity(searchText); 
		ArrayList<ArtistDTO> listDTO = new ArrayList<ArtistDTO>();
		for (Artist artist : list) {
			listDTO.add(ConverterFabric.getArtistDTO(artist));
		}
		return listDTO;
	}
}
